<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="hAssignPer(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEidet(row)">
                    编辑
                  </el-button>
                  <el-button size="small" type="danger" @click="dodel(row.id)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件
              current-page: 控制当前的页码值
              page-sizes 每页显示的条数的选项
              page-size 当前每页显示多少条
              total: 总条数
              layout :布局组件
               -->
              <el-pagination
                :current-page="pageParsms.page"
                :page-sizes="[2, 5, 10, 15, 20]"
                :page-size="pageParsms.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 这个是编辑的弹框 -->
      <el-dialog
        :title="isEidet ? '编辑角色' : '新增角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDidlog"
        @close="hClose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="dosubmit">
              确定
            </el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 这个事分配权限的弹框 -->
      <el-dialog :visible.sync="assignShowdialog" title="分配权限">
        <assignper v-if="assignShowdialog" :role-id="roleId" @success="assignShowdialog= false" />

      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRole, addRoles, editRoles, delRoles } from '@/api/settings'
import assignper from './assignPermission'
export default {
  components: {
    assignper
  },
  data() {
    return {
      roleId: '',
      assignShowdialog: false,
      isEidet: false,
      list: [], // 角色列表
      pageParsms: {
        page: 1, // 当前的页码值
        pagesize: 2 // 当前每页显示多少条
      },
      total: 0,
      showDidlog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    hAssignPer(id) {
      this.roleId = id
      this.assignShowdialog = true
    },
    // 清空表单
    hClose() {
      // 表单初始化
      this.$refs.roleForm.resetFields()
      // 将表单数据清空
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 删除
    dodel(id) {
      this.$confirm('是否删除此角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delRoles(id)
          console.log(res)
          if (this.list.length === 1) {
            this.pageParsms.page--
            if (this.pageParsms.page <= 0) {
              this.pageParsms.page = 1
            }
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 判断是否删除成功
          if (res.success) {
            // 重新获取页面
            this.loadRole()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 点击确定按钮,切换显示和是否编辑
    hAdd() {
      this.isEidet = false
      this.showDidlog = true
    },
    // 编辑角色
    hEidet(row) {
      this.roleForm = { ...row }
      this.showDidlog = true
      this.isEidet = true
    },
    async doEidet() {
      try {
        const res = await editRoles(this.roleForm)
        console.log(res)

        // 关闭弹窗
        this.showDidlog = false

        // 重新获取页面
        this.loadRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 确定按钮 新增角色名
    dosubmit() {
      // 表单兜底校验
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return
        this.isEidet ? this.doEidet() : this.doAdd()
      })
    },
    // 新增角色
    async doAdd() {
      try {
        const res = await addRoles(this.roleForm)
        console.log(res)
        // 将总数与服务器数据一致
        this.total++
        // 求出当前最大页码值
        const MaxNum = Math.ceil(this.total / this.pageParsms.pagesize)
        // 赋值
        this.pageParsms.page = MaxNum
        // 关闭弹窗
        this.showDidlog = false
        // 重新获取页面
        this.loadRole()
      } catch (error) {
        console.log(error)
      }
    },
    // 当每页显示的条数进行切换的时候触发    val 每页显示的条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageParsms.page = 1
      this.pageParsms.pagesize = val
      this.loadRole()
    },
    // 当亲的页码值进行切换时会触发,val:当前的页码值
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      // 改变请求参数的当前页码值
      this.pageParsms.page = val
      // 重新发送请求
      this.loadRole()
    },
    // 获取所有角色
    async loadRole() {
      try {
        const res = await getRole(this.pageParsms)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
